package com.bms.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.bms.tools.PictureTool;

@Controller
@RequestMapping("/upload")
public class FileUploadController extends BasicController {
	
	@ResponseBody
	@RequestMapping("/img/upload.json")
	public Object uploadImg(HttpServletRequest request,
			HttpServletResponse response) throws IllegalStateException, IOException {
		
		HashMap<String,String> pathMap = new HashMap<String,String>();

		String poses = request.getParameter("poses");
		JSONObject json = JSONObject.fromObject(poses);
		
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		// 判断 request 是否有文件上传,即多部分请求
		if (multipartResolver.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;

			// 取得request中的所有文件名
			Iterator<String> iter = multiRequest.getFileNames();
			while (iter.hasNext()) {
				
				// 取得上传文件
				MultipartFile file = multiRequest.getFile(iter.next());
				
				if(file != null){  
					
				    //取得当前上传文件的文件名称  
				    String myFileName = file.getOriginalFilename();  
				    //如果名称不为“”,说明该文件存在，否则说明该文件不存在  
				    if(myFileName.trim() !=""){  
				        
				    	// 记录上传过程起始时的时间，用来计算上传时间
						long pre = System.currentTimeMillis();
						
				        //重命名上传后的文件名  
				        String fileName = "CUT_"+pre+"_"+file.getOriginalFilename();  
				        String thumbFileName = "THUMB_"+pre+"_"+file.getOriginalFilename();  
				        
				        //定义上传路径  
				        String path = "/static/tmpImg/" + fileName; 
				        String tmpPath =request.getSession().getServletContext().getRealPath("/")+path;
				        
				        String thumbPath = "/static/tmpImg/" + thumbFileName; 
				        String tmpThumbPath =request.getSession().getServletContext().getRealPath("/")+thumbPath;
				        
				        //扩展名
				        String prefix = myFileName.substring(myFileName.indexOf(".")+1, myFileName.length());
				        
				        CommonsMultipartFile cf= (CommonsMultipartFile)file; 
				        DiskFileItem fi = (DiskFileItem)cf.getFileItem();                             
				        File srcFile = fi.getStoreLocation();
				        
				        //切图
				        File outFile = PictureTool.cutPic(srcFile, tmpPath, json,prefix);
				        
				        //缩略图
				        PictureTool.ThumbnailPic(outFile, tmpThumbPath, 0.1, prefix);
				        
				        pathMap.put("cutPath", path) ;
				        pathMap.put("thumbPath", thumbPath) ;
//				        File localFile = new File(tmpPath);  
//				        file.transferTo(localFile);  
				    }  
				}  

			}
		}
		
		return pathMap;
	}
}
